package com.ruoyi.system.controller;

import com.ruoyi.system.domain.SysBusinessInfo;
import com.ruoyi.system.domain.SysInsuranceDetails;
import com.ruoyi.system.domain.SysInsuranceweekDetails;
import com.ruoyi.system.mapper.SysBusinessInfoMapper;
import com.ruoyi.system.mapper.SysInsuranceDetailsMapper;
import com.ruoyi.system.mapper.SysInsuranceweekDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
public class ScheduleTask {

    @Autowired
    private SysBusinessInfoMapper sysBusinessInfoMapper;
    @Autowired
    private SysInsuranceDetailsMapper sysInsuranceDetailsMapper;
    @Autowired
    private SysInsuranceweekDetailsMapper sysInsuranceweekDetailsMapper;

    @Scheduled(cron = "0 0 0 * * ?")
    private void dailyTask() {
        try{
            sysBusinessInfoMapper.updateZeroTheNextDay();

            SysInsuranceDetails sysInsuranceDetails = new SysInsuranceDetails();
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat dfs=new SimpleDateFormat("yyyyMMdd");

            sysInsuranceDetails.setReportGenerationTime(df.parse(df.format(new Date())));
            String newestBatchNo = sysInsuranceDetailsMapper.selectNewestBatchNo();
            if(StringUtils.isEmpty(newestBatchNo)){
                sysInsuranceDetails.setBatchNo("P"+dfs.format(new Date()) +"A"+String.format("%03d",1));
            }else {
                if("A999".equals(newestBatchNo.substring(9,13))){
                    sysInsuranceDetails.setBatchNo("P"+dfs.format(new Date()) +"B"+String.format("%03d",1));
                }else {
                    sysInsuranceDetails.setBatchNo("P"+dfs.format(new Date()) +"A"+String.format("%03d",Integer.valueOf(newestBatchNo.substring(10,13)) + 1));
                }
            }

            List<SysBusinessInfo> list = sysBusinessInfoMapper.zeroTheNextDayList();
            sysInsuranceDetails.setOrderNum(String.valueOf(list.size()));
            sysInsuranceDetails.setTotalInsurancePremium(list.stream().map(SysBusinessInfo::getInsurancePremium).reduce(BigDecimal.ZERO, BigDecimal::add));
            int id = sysInsuranceDetailsMapper.insertSysInsuranceDetails(sysInsuranceDetails);
            sysBusinessInfoMapper.updateInsuranceDetailsId(list,id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 预投保状态的订单
     */
    @Scheduled(cron = "0 0 0 ? * FRI")
    private void cycleTask() {
        SysInsuranceweekDetails sysInsuranceweekDetails = sysInsuranceweekDetailsMapper.selectWeekDetailsList();
        String orderNum = sysInsuranceweekDetails.getOrderNum();
        if(orderNum.contains(".")) {
            int indexOf = orderNum.indexOf(".");
            orderNum = orderNum.substring(0, indexOf);
        }
        int matchRateInt = Integer.parseInt(orderNum);
        sysInsuranceweekDetails.setOrderNum(String.valueOf(matchRateInt));

        sysInsuranceweekDetails.setOrderNum(Integer.valueOf(sysInsuranceweekDetails.getOrderNum()).toString());

        List<SysInsuranceweekDetails> list = sysInsuranceweekDetailsMapper.selectSysInsuranceweekDetailsList(sysInsuranceweekDetails);
        if(null == list || list.size() ==0){
            int id = sysInsuranceweekDetailsMapper.insertSysInsuranceweekDetails(sysInsuranceweekDetails);

            String batchNoList = sysInsuranceweekDetails.getBatchNo();

            String[] as = batchNoList.split(",");
            for (int i = 0; i < as.length; i++) {
                SysInsuranceDetails sysInsuranceDetails = sysInsuranceDetailsMapper.selectSysInsuranceDetailsByBatchNo(as[i]);
                SysBusinessInfo sysBusinessInfo = sysBusinessInfoMapper.selectSysBusinessInfoByInsuranceDetailsId(sysInsuranceDetails.getId());

                sysBusinessInfoMapper.updateInsuranceweekDetailsId(sysBusinessInfo.getId(),Long.valueOf(String.valueOf(id)));
            }
        }
    }

}
